﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public enum CalcuType//枚举加减乘除
        {
            None,
            Addition,
            Substraction,
            Multiplication,
            Division,
        }
        private double? _ValueF = null;
        private double? _ValueL = null;//可以是NULL字符，也可以是数字
        private bool _IsNew = false;
        private int tag = 0;//用来判断写入第二个数字是否单数
        private bool _IsDot = false; //判断是否有小数点
        private int i = 0;
        private CalcuType _CalculateType = CalcuType.None;
        public Form1()
        {
            InitializeComponent();
        }
        private void btnVal_Click(object sender, EventArgs e) //用来读取数字
        {
            Button btn = (Button)sender;
            string numberStr = this.txtValue.Text;
            if (this._IsNew)//判断是否是第二个
            {
                if (tag == 0)
                {
                    numberStr = btn.Text;
                    tag++;
                }
                else
                {
                    if (btn.Text == ".")//判断第一个数输入是否包含小数
                    {
                        this._IsDot = true;
                        i++;
                    }

                    if (this._IsDot == false)//不包含小数
                    {
                        {
                            if (numberStr == "0" || numberStr == "0." || numberStr == "-0" || numberStr == "-0.")
                            {
                                numberStr = "";
                            }
                        }
                        numberStr += btn.Text;
                    }
                    else
                    { //包含小数,并判断是否再按小数点
                        if (i != 1)
                        {
                            numberStr = this.txtValue.Text;
                            i = 1;
                        }
                        else
                            numberStr += btn.Text;
                    }

                }

                this._ValueL = double.Parse(numberStr);
            }
            else
            {
                if (btn.Text == ".")//判断第一个数输入是否包含小数
                {
                    this._IsDot = true;
                    i++;
                }

                if (this._IsDot == false)//不包含小数
                {
                    {
                        if (numberStr == "0" || numberStr == "0." || numberStr == "-0" || numberStr == "-0.")
                        {
                            numberStr = "";
                        }
                    }
                    numberStr += btn.Text;
                }
                else
                { //包含小数,并判断是否再按小数点
                    if (i != 1)
                    {
                        numberStr = this.txtValue.Text;
                        i = 1;
                    }
                    else
                        numberStr += btn.Text;
                }
                this._ValueF = double.Parse(numberStr);
            }
            this.txtValue.Text = numberStr;
        }
        private void Form1_Load_1(object sender, EventArgs e)
        {
            btnval0.Click += new EventHandler(btnVal_Click);
            btnval1.Click += new EventHandler(btnVal_Click);
            btnval2.Click += new EventHandler(btnVal_Click);
            btnval3.Click += new EventHandler(btnVal_Click);
            btnval4.Click += new EventHandler(btnVal_Click);
            btnval5.Click += new EventHandler(btnVal_Click);
            btnval6.Click += new EventHandler(btnVal_Click);
            btnval7.Click += new EventHandler(btnVal_Click);
            btnval8.Click += new EventHandler(btnVal_Click);
            btnval9.Click += new EventHandler(btnVal_Click);
            btnDot.Click += new EventHandler(btnVal_Click);
        }
        private void btnAddition_Click(object sender, EventArgs e)
        {
            this.btnEqual_Click(sender, e);
            this._CalculateType = CalcuType.Addition;
            this._IsNew = true;
        }
        private void btnSubstraction_Click(object sender, EventArgs e)
        {
            this.btnEqual_Click(sender, e);
            this._CalculateType = CalcuType.Substraction;
            this._IsNew = true;
        }
        private void btnMultiplication_Click(object sender, EventArgs e)
        {
            this.btnEqual_Click(sender, e);
            this._CalculateType = CalcuType.Multiplication;
            this._IsNew = true;
        }
        private void btnDivision_Click(object sender, EventArgs e)
        {
            this.btnEqual_Click(sender, e);
            this._CalculateType = CalcuType.Division;
            this._IsNew = true;
        }
        private void btnPN_Click_1(object sender, EventArgs e)
        {
            this._ValueF = double.Parse(this.txtValue.Text);
            if (this._ValueF < 0)
                this.txtValue.Text = Math.Abs((double)this._ValueF).ToString();

            else
                this.txtValue.Text = ((double)(-this._ValueF)).ToString();
            this._ValueF = double.Parse(this.txtValue.Text);
            this._CalculateType = CalcuType.None;
        }
        private void btnDot_Click(object sender, EventArgs e)
        {
            this._ValueL = null;
            this._ValueF = null;
            this._CalculateType = CalcuType.None;
            this.txtValue.Text = "0.";
            this.tag = 0;
            this._IsDot = false;
            this.i = 0;
        }
        private void btnEqual_Click(object sender, EventArgs e)
        {
            switch (_CalculateType)
            {
                case CalcuType.Addition:
                    this.txtValue.Text = (_ValueF + _ValueL).ToString();
                    break;
                case CalcuType.Substraction:
                    this.txtValue.Text = (_ValueF - _ValueL).ToString();
                    break;
                case CalcuType.Multiplication:
                    this.txtValue.Text = (_ValueF * _ValueL).ToString();
                    break;
                case CalcuType.Division:
                    this.txtValue.Text = (_ValueF / _ValueL).ToString();
                    break;
            }
            this._ValueF = double.Parse(this.txtValue.Text);
            this._CalculateType = CalcuType.None;
            this._IsNew = false;
            this.tag = 0;
            this._IsDot = false;
            i = 0;
        }
        private void btnC_Click(object sender, EventArgs e)
        {
            this._ValueL = null;
            this._ValueF = null;
            this._CalculateType = CalcuType.None;
            this.txtValue.Text = "0.";
            this.tag = 0;
            this._IsDot = false;
            this.i = 0;

        }

        private void btnval1_Click(object sender, EventArgs e)
        {

        }
    }
}
